package com.cysion.study.widget.downmenu

import android.content.Context
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.CheckBox
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.cysion.study.R
import com.cysion.study.entity.Child


/**
 * Created by chu on 2019-07-14.
 */
class SimpleDropDownMenuAdapter1(private var context:Context, private val itemList: MutableList<MenuItem1>) : RecyclerView.Adapter<SimpleDropDownMenuAdapter1.VH>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VH {
        val view = LayoutInflater.from(parent?.context).inflate(R.layout.item_menu1, parent, false)
        return VH(view)

    }
    private var isShow = false
    private var isShowChild = false



    override fun onBindViewHolder(holder: VH, position: Int) {
        holder?.itemTitleTextView?.text = itemList[position].itemTitle
      /*  if (isShow) {
//        holder?.itemIconImgView?.imageResource = itemList[position].itemIcon
            holder.itemIconImgView.visibility = View.VISIBLE
        } else {
            holder.itemIconImgView.visibility = View.GONE
        }*/

//        mMenuItemAdapter = ChildMenuAdapter(mMenuItemList)

        if (isShow && pos == position) {
            holder.itemLinear.setBackgroundResource(R.drawable.btn_bg_gray)
        } else {
            holder.itemLinear.setBackgroundResource(R.color.transparent)
        }

        if (oneIconShow) {
            holder.itemIconImgView.visibility = View.GONE
            holder.cb_agree.visibility = View.VISIBLE
            holder.cb_agree.isChecked = itemList[position].isCheck
        } else {
            holder.cb_agree.visibility = View.GONE
            //判断子列表为空的话不显示一级列表图标
            /*if (itemList[position].child == null || itemList[position].child.size == 0) {
                holder.itemIconImgView.visibility = View.GONE
            } else {
                holder.itemIconImgView.visibility = View.VISIBLE
            }*/
            holder.itemIconImgView.visibility = View.VISIBLE
        }


        holder?.itemLinear?.setOnClickListener {
            itemClickListener?.onItemClick(holder.itemLinear,position)
         /*   mMenuItemList.clear()
            mMenuItemList=itemList[position].child
            mMenuItemAdapter?.notifyDataSetChanged()*/

        }
    }

    var itemClickListener: ItemClickListener? = null

    interface ItemClickListener {
        fun onItemClick(v: View, position: Int)
    }

    /*override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): VH {
        val view = LayoutInflater.from(parent?.context).inflate(R.layout.item_menu1, parent, false)
        return VH(view)
    }*/

    override fun getItemCount(): Int = itemList.size

    private var pos=-1
    //是否显示下拉选项图标
    fun showImg(b: Boolean,po:Int) {
        this.isShow = b
        this.pos=po
        notifyDataSetChanged()
    }

    private var oneIconShow=false
    //是否显示一级选择图标
    fun showOneIcon(b: Boolean) {
        this.oneIconShow = b
        notifyDataSetChanged()
    }



    /*override fun onBindViewHolder(holder: VH?, position: Int) {
        holder?.itemTitleTextView?.text = itemList[position].itemTitle
//        holder?.itemIconImgView?.imageResource = itemList[position].itemIcon
        holder?.itemLinear?.setOnClickListener {
            itemClickListener?.onItemClick(position)
        }
    }*/

    class VH internal constructor(view: View) : RecyclerView.ViewHolder(view) {
        var itemLinear = view.findViewById<LinearLayout>(R.id.item_linear)
//        var child_item_list = view.findViewById<RecyclerView>(R.id.child_item_list)
        var itemIconImgView = view.findViewById<ImageView>(R.id.item_img_icon)
        var cb_agree = view.findViewById<CheckBox>(R.id.cb_agree)
        var itemTitleTextView = view.findViewById<TextView>(R.id.item_txt_title)
    }
}

